package cn.com.huzo.webapp.controller.trade;

import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.trade.SalesContractHeaderEntity;
import cn.com.huzo.model.entities.trade.SalesContractLinesEntity;
import cn.com.huzo.service.trade.SalesContractHeaderManager;
import cn.com.huzo.service.trade.SalesContractLinesManager;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 售货合同控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/trade/sales_contract")
public class SalesContractController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "SalesContractHeaderManager")
    private SalesContractHeaderManager _manager_header;

    @Resource(name = "SalesContractLinesManager")
    private SalesContractLinesManager _manager_lines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到售货合同一览画面
     * <p>
     * @param request
     * @return
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.setViewName("trade.sales_contract.list.def");
        return view;
    }

    /**
     * 售货合同编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作标志 1;增加 2：编辑
     * @param headerId 售货合同标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/{action}/{headerId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "headerId") Long headerId) throws Exception {
        ModelAndView view = new ModelAndView();
        view.addObject("action", action);
        view.addObject("headerId", headerId.toString());
        view.setViewName("trade.sales_contract.edit.def");
        return view;
    }

    /**
     * 弹出售货合同选择画面
     * <p>
     * @return
     */
    @RequestMapping("/choice.html")
    public ModelAndView redirectSalesContractHeaderPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.trade.sales_contract.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表-数据处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "/header/fetchNewBillNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewBillNo() throws Exception {
        Map<String, Object> rtnVal = new HashMap();
        rtnVal.put("NoOfBill", _manager_header.fetchNewBillNo());
        return rtnVal;
    }

    /**
     * 根据标识取得售货合同数据。
     *
     * @param key 产品标识
     * @return 售货合同数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody final Long key) throws Exception {
        Map<String, Object> _result = _manager_header.fetchByHeaderKey(key);
        _result = _result == null ? new HashMap() : _result;

        return _result;
    }

    /**
     * 删除售货合同计划数据。
     *
     * @param criterion 产品标识
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }

    /**
     * 根据条件取得售货合同分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 售货合同分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 保存售货合同数据。
     * <p>
     * @param instance 要保存的售货合同对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody SalesContractHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { //编辑
            rowsAffected = (long) _manager_header.update(instance);
        } else {//新增
            rowsAffected = _manager_header.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 审核售货合同数据。
     * <p>
     * @param instance 要审核售货合同对象。
     * @return 影响行数.
     * @throws Exception
     */
    @RequestMapping(value = "/check.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer check(@RequestBody SalesContractHeaderEntity instance) throws Exception {
        return _manager_header.doCheck(instance);

    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细-数据处理部分">
    /**
     * 根据主表标识取得售货合同明细数据。
     *
     * @param headerId 产品标识
     * @return 售货合同明细数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchLinesByHeaderKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchLinesByHeaderKey(@RequestBody final Long headerId) throws Exception {
        Map<String, Object> _result = _manager_header.fetchLinesByHeaderKey(headerId);
        _result = _result == null ? new HashMap() : _result;

        return _result;
    }

    /**
     * 增加或者更新售货合同计划明细数据。
     *
     * @param criterion 要增加或者更新的明细数据
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> savelines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.batchUpdate(criterion);
    }

    /**
     * 删除售货合同计划明细数据。
     *
     * @param criterion 要删除的数据
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteOne(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }
    // </editor-fold>
}
